﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="父级" prop="ParentId">
          <!--<a-input v-model="entity.ParentId" autocomplete="off" placeholder="为空时为电梯主分类，有值的为电梯下辖的分类" />-->
          <a-tree-select
            v-model="entity.ParentId"
            show-search="false"
            autoClearSearchValue="true"
            :value="value"
            :not-found-content="null"
            tree-node-filter-prop="title"
            :treeData="ParentIdTreeData"
            placeholder="请选择父级电梯信息"
            searchPlaceholder="请输入要查询的关键字"
            treeDefaultExpandAll
            allowClear
            :dropdown-style="{ maxHeight: '280px',overflow: 'auto' }"
            :getPopupContainer="triggerNode => { return triggerNode.parentNode || document.body;}"
          ></a-tree-select>
        </a-form-model-item>
        <a-form-model-item label="排序" prop="Sort">
          <a-input v-model="entity.Sort" autocomplete="off" placeholder="越小越靠前" />
        </a-form-model-item>
        <a-form-model-item label="电梯类型" prop="Title">
          <a-input v-model="entity.Title" autocomplete="off" placeholder="枚举值" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-input v-model="entity.Remark" autocomplete="off" placeholder="备注" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    // eslint-disable-next-line vue/require-default-prop
    parentObj: Object
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      ParentIdTreeData: [],
      visible: false,
      loading: false,
      entity: {},
      rules: {
        Title: [{ required: true, message: '必填' }]
        // ParentId: [{ required: true, message: '必填' }]
      },
      title: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      this.loading = true
      this.$http.post('/Base_BasicData/Base_ElevatorType/GetTreeDataList', {}).then((resJson) => {
        this.loading = false
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
      if (id) {
        this.loading = true
        this.$http.post('/Base_BasicData/Base_ElevatorType/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        if (this.entity.ParentId == null) {
          alert(this.entity.ParentId)
          this.entity.ParentId = 'top'
        }
        this.$http.post('/Base_BasicData/Base_ElevatorType/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
